package org.example.proxy;

import org.example.RPCApplication;
import org.example.config.RPCConfig;

import java.lang.reflect.Proxy;

public class UserServiceFactory {
    public static <T> T getUserService(Class<T> clazz) {
        if(RPCApplication.getConfig().isMock()) {
            return getMockProxy(clazz);
        }
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[]{clazz},
                new UserServiceDynamicProxy());
    }

    // 根据服务类获取代理对象
    public static <T> T getMockProxy(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class[]{clazz},
                new MockServiceProxy());
    }
}
